

package com.example.moshudriver.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.BeeFramework.model.BaseModel;
import com.BeeFramework.model.BeeCallback;
import com.example.moshudriver.protocol.ApiInterface;
import com.example.moshudriver.protocol.LOCATION;
import com.example.moshudriver.protocol.ORDER_INFO;
import com.example.moshudriver.protocol.orderlistaroundRequest;
import com.example.moshudriver.protocol.orderlistaroundResponse;
import com.example.moshudriver.tools.LocationManager;
import com.example.moshudriver.tools.O2OMobileAppConst;
import com.example.moshudriver.tools.SESSION;
import com.external.androidquery.callback.AjaxStatus;

public class OrderListAroundModel extends BaseModel {
//司机代驾的请求列表--乘客
	public static final int NUMPERPAGE = 10;
	public ArrayList<ORDER_INFO> publicOrderList = new ArrayList<ORDER_INFO>();
	public int publicMore;
	
	public OrderListAroundModel(Context context) {
		super(context);
	}
	
	public void getList(int sort_by) {
        orderlistaroundRequest request = new orderlistaroundRequest();
		request.sid = SESSION.getInstance().sid;
		request.uid = SESSION.getInstance().uid;
		request.ver = O2OMobileAppConst.VERSION_CODE;
		request.sort_by = sort_by;
		request.by_no = 1;
		request.count = NUMPERPAGE;
		LOCATION location = new LOCATION();
		location.lat = LocationManager.getLatitude();
		 Log.i("纬度", location.lat+"");
		location.lon = LocationManager.getLongitude();
		 Log.i("经度", location.lon+"");
		request.location = location;
		
		BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

			@Override
			public void callback(String url, JSONObject jo, AjaxStatus status) {
				try {
					OrderListAroundModel.this.callback(this, url, jo, status);
                    if (null != jo) {
                        orderlistaroundResponse response = new orderlistaroundResponse();
                        response.fromJson(jo);
                        publicMore = response.more;
                        if(response.succeed == 1) {
                            publicOrderList.clear();
                            publicOrderList.addAll(response.orders);
                        	OrderListAroundModel.this.OnMessageResponse(url,jo,status);
                        } else {
                        	OrderListAroundModel.this.callback(url, response.error_code, response.error_desc);
                        }
                    }
				} catch (JSONException e) {

				}
			}
		};

		Map<String, Object> params = new HashMap<String, Object>();
		try {
            JSONObject requestJson = request.toJson();
            requestJson.remove("by_id");
			params.put("json", requestJson.toString());
		} catch (JSONException e) { 
			
		}
        if(isSendingMessage(ApiInterface.ORDERLIST_AROUND)){
               return;
        }
		cb.url(ApiInterface.ORDERLIST_AROUND).type(JSONObject.class).params(params);
		ajaxProgress(cb);
	}
	
	public void getListMore(int sort_by) {
        orderlistaroundRequest request = new orderlistaroundRequest();
		request.sid = SESSION.getInstance().sid;
		request.uid = SESSION.getInstance().uid;
		request.ver = O2OMobileAppConst.VERSION_CODE;
		request.sort_by = sort_by;
		request.by_no = (int)Math.ceil(publicOrderList.size()*1.0/NUMPERPAGE) +1;
		request.count = NUMPERPAGE;
		LOCATION location = new LOCATION();
		location.lat = LocationManager.getLatitude();
		location.lon = LocationManager.getLongitude();
		request.location = location;
		
		BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

			@Override
			public void callback(String url, JSONObject jo, AjaxStatus status) {
				try {
					OrderListAroundModel.this.callback(this, url, jo, status);
                    if (null != jo) {
                        orderlistaroundResponse response = new orderlistaroundResponse();
                        response.fromJson(jo);
                        publicMore = response.more;
                        if(response.succeed == 1) {
                            publicOrderList.addAll(response.orders);
                        	OrderListAroundModel.this.OnMessageResponse(url,jo,status);
                        } else {
                        	OrderListAroundModel.this.callback(url, response.error_code, response.error_desc);
                        }
                    }
				} catch (JSONException e) {

				}
			}
		};

		Map<String, Object> params = new HashMap<String, Object>();
		try {
            JSONObject requestJson = request.toJson();
            requestJson.remove("by_id");
			params.put("json", requestJson.toString());
		} catch (JSONException e) { 
			
		}
        if(isSendingMessage(ApiInterface.ORDERLIST_AROUND)){
            return;
        }
		cb.url(ApiInterface.ORDERLIST_AROUND).type(JSONObject.class).params(params);
		ajaxProgress(cb);
	}
	
}
